from __future__ import absolute_import, division

from gui.panda.base import *


class PandaBuildBar(PandaUIElement):
    texture_name =_ ('UI Build Bar')

    build_button   = Attribute()
    destroy_button = Attribute()
    
    def setup_element(self):
        self.build_button   = self.add_child(StateButton('bar.build.button.build',   (20, 21), icon = 'Hammer', action = self.model.show_buildings))
        self.destroy_button = self.add_child(StateButton('bar.build.button.destroy', (50, 21), icon = 'Cross',  action = self.model.start_destroying))

    def post_update(self):
        self.build_button  .is_locked_in = self.model.is_showing_buildings
        self.destroy_button.is_locked_in = self.model.is_destroying


class PandaBuildingsPanel(PandaUIElement):
    texture_name      =_ ('UI Buildings Panel')
    button_height     =_ ( 30)
    button_width      =_ ( 32)
    button_offset     =_ ( 20)
    inner_width       =_ (200)
    label_margin      =_ (  5)
    label_height      =_ ( 28)
    label_font_size   =_ ( 16)
    label_icon_offset =_ ( 10,  5)
    bar_offset        =_ (127, -8)
    bar_margin        =_ (  4)
    top_offset        =_ (-12)
    
    tab_folder = Attribute()
    buttons    = Attribute()
    
    @property
    def current_building(self):
        if self.buttons.has_current:
            return self.buttons.current.data
        else:
            return None
    
    def clear_building(self):
        self.buttons.set_current(None)
    
    def setup_element(self):
        self.buttons    = ButtonGroup()
        self.tab_folder = self.add_child(TabFolder('panel.buildings.tabs', (20, 65),
                                                   tab_position = (10, -38),
                                                   tab_delta    = 40))
        
        for category in self.model.categories:
            tab  = self.tab_folder.add_tab(category.id, category.icon)
            x    = 0
            y    = self.top_offset
            last = None
            
            for subcategory in plural(category.subcategories):
                y += self.label_margin
                
                tab.add_item(Heading('panel.buildings.heading.' + subcategory.id, (x, y),
                                     font_size = self.label_font_size,
                                     text      = subcategory.name,
                                     icon      = subcategory.icon,
                                     offset    = self.label_icon_offset))
                
                y += self.label_height
                
                for building in plural(subcategory.buildings):
                    if x > self.inner_width:
                        x  = 0
                        y += self.button_height
                    
                    tab.add_item(StateButton('panel.buildings.building.' + building.name.lower(),
                                             (x + self.button_offset, y),
                                             icon  = building.name,
                                             group = self.buttons,
                                             data  = building))
                    
                    x += self.button_width
                
                x  = 0
                y += self.button_height
                
                
                last = tab.add_item(Icon('panel.buildings.bar.' + subcategory.id,
                                         (x + self.bar_offset[0], y + self.bar_offset[1]),
                                         icon = 'UI Buildings Panel Bar'))
                
                y += self.bar_margin
            
            if last is not None:
                last.hide()
        
        self.hide()
    
    def on_update(self):
        if self.model.is_visible:
            self.show()
        else:
            self.hide()
